from django.db import models

class SiteUser(models.Model):
    login = models.CharField(max_length=50)
    password_hash = models.CharField(max_length=16,blank=True)
    email = models.EmailField(blank=True)
    registered = models.DateField(auto_now=True)
    def __unicode__(self):
        return u'%s' % self.login
    def __str__(self):
        return u'%s' % self.login

    #TODO: this class is fake now
class VKUser(models.Model):
    vkprofile = models.CharField(max_length=50)
    vkname = models.CharField(max_length=50)
    def __unicode__(self):
        return u'%s' % self.vkname
    def __str__(self):
        return u'%s' % self.vkname

class RegisteredUser(models.Model):
    siteuser = models.ForeignKey(SiteUser, blank=True, null=True)
    vkuser = models.ForeignKey(VKUser, blank=True, null=True)
    allowedToComment = models.BooleanField(default=False)
    allowedToPost = models.BooleanField(default=False)
    allowedToAddLink = models.BooleanField(default=False)
    lastlogin = models.DateField(auto_now=True)
    def __unicode__(self):
        if self.siteuser is not None:
            return self.siteuser.login
        return u'(%d)' % self.id
    def __str__(self):
        if self.siteuser is not None:
            return self.siteuser.login
        return u'(%d)' % self.id

class Tag(models.Model):
    name = models.CharField(max_length=50)
    def __unicode__(self):
        return self.name
    def __str__(self):
        return self.name

class Category(models.Model):
    name = models.CharField(max_length=50)
    def __unicode__(self):
        return self.name
    def __str__(self):
        return self.name

class Post(models.Model):
    author = models.ForeignKey(RegisteredUser)
    title = models.CharField(max_length=512)
    tags = models.ManyToManyField(Tag, blank=True, null=True)
    category = models.ForeignKey(Category, blank=True, null=True)
    created = models.DateField(auto_now=True)
    content = models.TextField()
    isPublic = models.BooleanField(default=False)
    isModerated = models.BooleanField(default=False)
    def __unicode__(self):
        return self.title
    def __str__(self):
        return self.title
    def __lt__(self, y):
        return self.created < y.created or self.id < y.id
    def itemtype(self): return "Post"

class PostComment(models.Model):
    author = models.ForeignKey(RegisteredUser)
    post = models.ForeignKey(Post)
    tags = models.ManyToManyField(Tag, blank=True, null=True)
    created = models.DateField(auto_now=True)
    content = models.TextField()
    isModerated = models.BooleanField(default=True)
    def __unicode__(self):
        return u'%s#%d' % (self.post.title, self.id)
    def __str__(self):
        return u'%s#%d' % (self.post.title, self.id)
    def __lt__(self, y):
        return self.created < y.created or self.id < y.id

class Link(models.Model):
    author = models.ForeignKey(RegisteredUser)
    title = models.CharField(max_length=512)
    url = models.CharField(max_length=512)
    description = models.TextField(blank=True)
    tags = models.ManyToManyField(Tag, blank=True, null=True)
    category = models.ForeignKey(Category, blank=True, null=True)
    created = models.DateField(auto_now=True)
    isPublic = models.BooleanField(default=False)
    isModerated = models.BooleanField(default=False)
    def __unicode__(self):
        return u'%s %s' % (self.url, self.title)
    def __str__(self):
        return u'%s %s' % (self.url, self.title)
    def __lt__(self, y):
        return self.created < y.created or self.id < y.id
    def itemtype(self): return "Link"
